(() => {
	let ele = {};
	ele.pwd = $('#password');
	ele.phone = $('#mobile');
	ele.login = $('#login');
	ele.agree = $('#agree');
	
	ele.explain = $('#explain');
	
	
	// 3、用于登录底部用户协议被软键盘顶起 解决办法
	
	 // 解决底部元素被键盘顶起问题
	 
	 let screenHeight = document.body.offsetHeight
	 // 为window绑定resize事件
	 window.onresize = function () {
	  let nowHeight = document.body.offsetHeight
	  if (nowHeight < screenHeight) {
	   // 将底部弹起的按钮隐藏(可使用给按钮添加相应类)
	   ele.explain.style.display = 'none'
	  } else {
	   // 将按钮正常显示(可使用给按钮移除相应类)
	   ele.explain.style.display = 'block'
	  }
	 }
	
	//登录
	ele.login.onclick = () => {
		let phone = ele.phone.value;
		let pwd = ele.pwd.value;
		let agree = ele.agree.checked;
		if (!phone) {
			openTips('账号不能为空');
			return false;
		}
		else if (!/^1[3-9]\d{9}$/.test(phone)) {
			openTips('账号有误');
			return false;
		}
		else if (!pwd) {
			openTips('密码不能为空');
			return false;
		}
		else if (!/\w{6,12}/.test(pwd)) {
			openTips('密码为6~12位字符');
			return false;
		}
		else if (!agree) {
			openTips('您需要同意用户协议和隐私政策');
			return false;
		}

		ajax({
			url: API_PATH.LOGIN,
			method: 'POST',
			data: {
				account: phone,
				pwd: pwd,
			},
			success: res => {
				console.log(res);
				if (res.code == 1) {
					openTips(res.msg);
					let dt = 365 * 24 * 60 * 60;
					setCookie('token', res.data.token, dt);
					setCookie('mobile', res.data.user.mobile, dt);
					setCookie('userid',res.data.user.id, dt);
					if(res.data.user.ip === '' || res.data.user.ip === null) {
						setTimeout(() => {
							location.replace('optionRole.html');
						}, 1000)
					} else {
						setTimeout(() => {
							location.replace('home.html');
						}, 1000)
					}
				} else {
					openTips(res.msg || '未知错误')
				}
			}
		})
	}


})();
